################################################################################
## libasyncd
##
## Copyright (c) 2014 Seungyoung Kim.
## All rights reserved.
##
## Redistribution and use in source and binary forms, with or without
## modification, are permitted provided that the following conditions are met:
##
## 1. Redistributions of source code must retain the above copyright notice,
##    this list of conditions and the following disclaimer.
## 2. Redistributions in binary form must reproduce the above copyright notice,
##    this list of conditions and the following disclaimer in the documentation
##    and/or other materials provided with the distribution.
##
## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
## AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
## IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
## ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
## LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
## CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
## SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
## INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
## CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
## ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
## POSSIBILITY OF SUCH DAMAGE.
################################################################################

prefix		= @prefix@
exec_prefix	= @exec_prefix@

## Utilities
AR		= @AR@
ARFLAGS		= -rc
CHMOD		= @CHMOD@
INSTALL		= @INSTALL@
INSTALL_DATA	= @INSTALL_DATA@
LN_S		= @LN_S@
MKDIR_P		= @MKDIR_P@
RMDIR		= rmdir
RANLIB		= @RANLIB@
RM		= @RM@

## Install directories
INST_INCDIR	= @includedir@
INST_LIBDIR	= @libdir@

## qlibc related.
QLIBC_DIR	= ../lib/qlibc
QLIBC_LIB	= $(QLIBC_DIR)/lib/libqlibc.a
CPPFLAGS	+= -I$(QLIBC_DIR)/include
LIBS		+= $(QLIBC_LIB)
QLIBC		= embedded

## libasyncd related.
HEADERDIR	= ../include/asyncd
CPPFLAGS	+= -I$(HEADERDIR)
OBJS		= ad_server.o ad_http_handler.o
LIBNAME		= libasyncd.a
SLIBNAME	= libasyncd.so.1
SLIBNAME_LINK	= libasyncd.so

## Which compiler & options for release
CC		= gcc -std=gnu99
CFLAGS		= -Wall -Wstrict-prototypes -fPIC -g -O2
CPPFLAGS	+=  -I/usr/include -I/usr/local/include \
		   -D_GNU_SOURCE -DBUILD_DEBUG -D_FILE_OFFSET_BITS=64
LIBS		+= @DEPLIBS@

## Make Library

ifeq ($(QLIBC),system)
all: $(LIBNAME)
else
all: qlibc $(LIBNAME)
endif

$(LIBNAME): $(OBJS)
	$(AR) $(ARFLAGS) $(LIBNAME) $(OBJS)
	$(RANLIB) $(LIBNAME)
	$(CC) -shared -Wl,-soname,$(SLIBNAME) -o $(SLIBNAME) $(OBJS)
	$(LN_S) -f $(SLIBNAME) $(SLIBNAME_LINK)

qlibc:
	@if [ ! -d $(QLIBC_DIR)/src ]; then \
	        ../lib/run2init-submodules.sh; \
	fi
	@if [ ! -f $(QLIBC_LIB) ]; then \
	        (cd $(QLIBC_DIR) && ./configure && make); \
	fi

install: install-libasyncd

install-libasyncd: all
	$(MKDIR_P) $(DESTDIR)/$(INST_INCDIR)/asyncd
	$(INSTALL_DATA) $(HEADERDIR)/asyncd.h $(DESTDIR)/$(INST_INCDIR)/asyncd/asyncd.h
	$(INSTALL_DATA) $(HEADERDIR)/ad_server.h $(DESTDIR)/$(INST_INCDIR)/asyncd/ad_server.h
	$(INSTALL_DATA) $(HEADERDIR)/ad_http_handler.h $(DESTDIR)/$(INST_INCDIR)/asyncd/ad_http_handler.h
	$(MKDIR_P) $(DESTDIR)/$(INST_LIBDIR)
	$(INSTALL_DATA) $(LIBNAME) $(DESTDIR)/$(INST_LIBDIR)/$(LIBNAME)
	$(INSTALL_DATA) $(SLIBNAME) $(DESTDIR)/$(INST_LIBDIR)/$(SLIBNAME)
	( cd $(DESTDIR)/$(INST_LIBDIR); $(LN_S) -f $(SLIBNAME) $(SLIBNAME_LINK) )

uninstall-libasyncd:
	$(RM) -rf $(DESTDIR)/$(INST_INCDIR)/asyncd
	$(RM) -f $(DESTDIR)/$(INST_LIBDIR)/$(LIBNAME)
	$(RM) -f $(DESTDIR)/$(INST_LIBDIR)/$(SLIBNAME)
	$(RM) -f $(DESTDIR)/$(INST_LIBDIR)/$(SLIBNAME_LINK)

deinstall: uninstall
uninstall: uninstall-libasyncd
	$(RMDIR) $(DESTDIR)/$(INST_INCDIR)/asyncd

clean:
	$(RM) -f $(OBJS) $(LIBNAME) $(SLIBNAME) $(SLIBNAME_LINK)

doc:
	doxygen doxygen.conf

cleandoc:
	$(RM) -rf ../doc/html

## Compile
.c.o:
	$(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<
